<?php


namespace tmcore\traits;


trait DataTrait
{
    /**
     * 设置搜索条件
     *
     * @param array $search
     * @param array $extend
     * @return array
     */
    protected static function getSearchData(array $search, $extend = [], $data = []): array
    {
        $params = request()->param();
        if (empty($search)) {
            return [];
        }
        if($data) $params = array_merge($params, $data);

        $where = [];
        foreach ($search as $whereType => $whereFields) {
            switch ($whereType) {
                case '=':
                case '<>':
                case '>':
                case '>=':
                case '<':
                case '<=':
                case 'in':
                    foreach ($whereFields as $whereField) {
                        $paramsName = strpos($whereField, '@') ? explode('@', $whereField) : $whereField;
                        $key = is_array($paramsName) ? $paramsName[0] : $paramsName; //参数的名称
                        $val = is_array($paramsName) ? $paramsName[1] : $whereField; //字段的名称
                        if (!isset($params[$key]) || (empty($params[$key]) && !is_numeric($params[$key]))) {
                            continue;
                        }
                        $where[] = [$val, $whereType, $params[$key]];
                    }
                    break;
                case '%like%':
                    foreach ($whereFields as $whereField) {
                        $paramsName = strpos($whereField, '@') ? explode('@', $whereField) : $whereField;
                        $key = is_array($paramsName) ? $paramsName[0] : $paramsName;
                        $val = is_array($paramsName) ? $paramsName[1] : $whereField;
                        if (!isset($params[$key]) || (empty($params[$key]) && !is_numeric($params[$key]))) {
                            continue;
                        }
                        $where[] = [$val, 'like', '%' . $params[$key] . '%'];
                    }
                    break;
                case '%like':
                    foreach ($whereFields as $whereField) {
                        $paramsName = strpos($whereField, '@') ? explode('@', $whereField) : $whereField;
                        $key = is_array($paramsName) ? $paramsName[0] : $paramsName;
                        $val = is_array($paramsName) ? $paramsName[1] : $whereField;
                        if (!isset($params[$key]) || (empty($params[$key]) && !is_numeric($params[$key]))) {
                            continue;
                        }
                        $where[] = [$val, 'like', '%' . $params[$key]];
                    }
                    break;
                case 'like%':
                    foreach ($whereFields as $whereField) {
                        $paramsName = strpos($whereField, '@') ? explode('@', $whereField) : $whereField;
                        $key = is_array($paramsName) ? $paramsName[0] : $paramsName;
                        $val = is_array($paramsName) ? $paramsName[1] : $whereField;
                        if (!isset($params[$key]) || (empty($params[$key]) && !is_numeric($params[$key]))) {
                            continue;
                        }
                        $where[] = [$val, 'like', $params[$key]];
                    }
                    break;
                case 'between':
                    foreach ($whereFields as $whereField => $paramsValue) {
                        $paramsName = explode('|', $paramsValue);
                        if (empty($params[$paramsName[0]]) || empty($params[$paramsName[1]])) {
                            break;
                        }
                        $start = $params[$paramsName[0]];
                        $end   = $params[$paramsName[1]];
                        $where[] = [$whereField, 'between', [$start, $end]];
                    }
                    break;
                case 'datetime':
                    foreach ($whereFields as $whereField) {
                        $paramsName = strpos($whereField, '@') ? explode('@', $whereField) : $whereField;
                        $key = is_array($paramsName) ? $paramsName[0] : $paramsName;
                        $val = is_array($paramsName) ? $paramsName[1] : $whereField;
                        if (!isset($params[$key]) || empty($params[$key])) {
                            continue;
                        }

                        list($start, $end) = explode(' - ', $params[$key]);
                        $where[] = [$val, '>=', strtotime($start)];
                        $where[] = [$val, '<=', strtotime($end)];
                    }
                    break;
                case 'find_in_set':
                    foreach ($whereFields as $whereField) {
                        $paramsName = strpos($whereField, '@') ? explode('@', $whereField) : $whereField;
                        $key = is_array($paramsName) ? $paramsName[0] : $paramsName;
                        $val = is_array($paramsName) ? $paramsName[1] : $whereField;
                        if (!isset($params[$key]) || (empty($params[$key]) && !is_numeric($params[$key]))) {
                            continue;
                        }
                        $where[] = [$val, 'find in set', $params[$key]];
                    }
                    break;
            }
        }
        if($extend && is_array($extend)) {
            if(is_array($extend[0])) {
                foreach ($extend as $extend_item) {
                    $where[] = $extend_item;
                }
            } else {
                $where[] = $extend;
            }
        }
        return $where;
    }

    /**
     * 获取分页参数
     * @param array $params
     * @return array
     */
    protected static function getPageData($params = []) {
        return [
            'page' => request()->param('page_num', 1),
            'limit' =>  request()->param('page_limit', 15),
            'query' => request()->param('page_query', $params?:[])
        ];
    }

    /**
     * 获取导出参数
     * @return array
     */
    protected static function getExportData($params = []) {
        return [
            'operation_type' => 'TM_EXCEL_EXPORT',
            'export_type' => $params['export_type']??request()->param('export_type', 1),
            'is_page' =>  $params['is_page']??request()->param('is_page', 0),
            'is_async' =>  $params['is_async']??request()->param('is_async', 0),
            'page_start' =>  $params['page_start']??request()->param('page_start', 1),
            'page_end' =>  $params['page_end']??request()->param('page_end', 200),
            'page_limit' =>  $params['page_limit']??request()->param('page_limit', 15),
            'file_name' =>  $params['file_name']??request()->param('file_name', ''),
            'file_suffix' =>  $params['file_suffix']??request()->param('file_suffix', ''),
            'file_random' =>  $params['file_random']??request()->param('file_random', ''),
            'params' =>  $params['params']??request()->param(),
            'file_identifier' => $params['file_identifier']??request()->param('file_identifier', '')
        ];
    }

    /**
     * 获取get请求数据
     *
     * @param array $params 参数
     * @param bool $suffix 是否按数组排序返回
     * @return array
     */
    protected function getData($params = [], $suffix = false)
    {
        if (!$params) {
            return $this->request->get();
        } else {
            $data = [];
            if(is_array($params)) {
                $i = 0;
                foreach ($params as $param) {
                    if (!is_array($param)) {
                        $data[$suffix == true ? $i++ : $param] = $this->request->get($param);
                    } else {
                        if(empty($param[0])) continue;
                        if(!isset($param[1])) $param[1] = null;
                        if(empty($param[2])) $param[2] = "";
                        $data[$suffix == true ? $i++ : $param[0]] = $this->request->get($param[0], $param[1], $param[2]);
                    }
                }
            }
            return $data;
        }
    }

    /**
     * 获取post请求数据
     *
     * @param array $params 参数
     * @param bool $suffix 是否按数组排序返回
     * @return array
     */
    protected function postData($params = [], $suffix = false)
    {
        if (!$params) {
            return $this->request->post();
        } else {
            $data = [];
            if(is_array($params)) {
                $i = 0;
                foreach ($params as $param) {
                    if (!is_array($param)) {
                        $data[$suffix == true ? $i++ : $param] = $this->request->post($param);
                    } else {
                        if(empty($param[0])) continue;
                        if(!isset($param[1])) $param[1] = null;
                        if(empty($param[2])) $param[2] = "";
                        $data[$suffix == true ? $i++ : $param[0]] = $this->request->post($param[0], $param[1], $param[2]);
                    }
                }
            }
            return $data;
        }
    }

    /**
     * 获取delete请求数据
     *
     * @param array $params 参数
     * @param bool $suffix 是否按数组排序返回
     * @return array
     */
    protected function deleteData($params = [], $suffix = false)
    {
        if (!$params) {
            return $this->request->delete();
        } else {
            $data = [];
            if(is_array($params)) {
                $i = 0;
                foreach ($params as $param) {
                    if (!is_array($param)) {
                        $data[$suffix == true ? $i++ : $param] = $this->request->delete($param);
                    } else {
                        if(empty($param[0])) continue;
                        if(!isset($param[1])) $param[1] = null;
                        if(empty($param[2])) $param[2] = "";
                        $data[$suffix == true ? $i++ : $param[0]] = $this->request->delete($param[0], $param[1], $param[2]);
                    }
                }
            }
            return $data;
        }
    }

    /**
     * 获取param请求数据
     *
     * @param array $params 参数
     * @param bool $suffix 是否按数组排序返回
     * @return array
     */
    protected function paramData($params = [], $suffix = false)
    {
        if (!$params) {
            return $this->request->param();
        } else {
            $data = [];
            if(is_array($params)) {
                $i = 0;
                foreach ($params as $param) {
                    if (!is_array($param)) {
                        $data[$suffix == true ? $i++ : $param] = $this->request->param($param);
                    } else {
                        if(empty($param[0])) continue;
                        if(!isset($param[1])) $param[1] = null;
                        if(empty($param[2])) $param[2] = "";
                        $data[$suffix == true ? $i++ : $param[0]] = $this->request->param($param[0], $param[1], $param[2]);
                    }
                }
            }
            return $data;
        }
    }
}